package com.gameObjects;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Input.TouchEvent;
import com.badlogic.androidgames.framework.gl.Camera2D;
import com.badlogic.androidgames.framework.gl.SpriteBatcher;
import com.badlogic.androidgames.framework.math.OverlapTester;
import com.baseinfinityrun.GameScreen;

public abstract class Era
{
	public Player player;
	protected Background lastBg, actualBg;
	protected List<TouchEvent> touchEvents;
	protected int actualEraID;
	protected List<Obstacle> obstacles;
	protected InputStream stream;
	
	public Era(Player p, Background b, Game g)
	{
		this.player = p;
		this.lastBg = b;
		this.actualBg = b;
		this.actualBg.image = this.lastBg.image;
		this.actualBg.position.set(this.lastBg.position.x + this.lastBg.size.x/2 , this.lastBg.position.y);
	}
	
	public abstract void Update(float deltaTime);
	public abstract void Draw(SpriteBatcher batcher);
	
	public abstract String CollisionObstacles();
	
	public void ReceiveTouch(List<TouchEvent> touchs)
	{
		touchEvents = touchs;
	}
	
	public void ChangeCamera(Camera2D camera)
	{
		camera.setViewportAndMatrices();
		camera.position.x = player.position.x;
	}
	
	public void LoadObstacle(InputStream stream)
	{
		this.obstacles = new ArrayList<Obstacle>();
		Obstacle obstacle = null;
		String text = null, tagName = null;
		
		XmlPullParserFactory factory = null;
		XmlPullParser parser = null;
		
		try
		{
			factory = XmlPullParserFactory.newInstance();
			factory.setNamespaceAware(true);
			parser = factory.newPullParser(); 
			
			parser.setInput(stream, null);
						
			int evType = parser.getEventType();
			
			while(evType != XmlPullParser.END_DOCUMENT)
			{
				tagName = parser.getName();
				switch(evType)
				{
					case XmlPullParser.START_TAG:
						if(tagName.equalsIgnoreCase("barrier"))
						{
							obstacle = new Barrier();
						}
						
						else if(tagName.equalsIgnoreCase("portal"))
						{
							obstacle = new Portal();
						}
						break;
					
					case XmlPullParser.TEXT:
						text = parser.getText();
						break;
						
					case XmlPullParser.END_TAG:
						if(tagName.equalsIgnoreCase("barrier") || tagName.equalsIgnoreCase("portal"))
						{
							obstacle.InstantiateObstacle();
							this.obstacles.add(obstacle);
						}
						
						else if(tagName.equalsIgnoreCase("x"))
						{
							obstacle.SetX(Float.parseFloat(text));
						}
						
						else if(tagName.equalsIgnoreCase("y"))
						{
							obstacle.SetY(Float.parseFloat(text));
						}
						
						else if(tagName.equalsIgnoreCase("w"))
						{
							obstacle.SetW(Float.parseFloat(text));
						}
						
						else if(tagName.equalsIgnoreCase("h"))
						{
							obstacle.SetH(Float.parseFloat(text));
						}
						break;
						
					default:
						break;
				
				}
				evType = parser.next();
			}
		}
		
		catch(XmlPullParserException e)
		{
			Log.d("testes", "erro por: " + e.getMessage()); 
		}
		catch (IOException e) {
			Log.d("testes", "erro por: " + e.getMessage()); 
		}
	}
	
	public void ChangeBg()
	{
		if(player.position.x + player.rectCollider.width/2 > actualBg.position.x)
		{
			switch(this.actualEraID)
			{
				case 0:
					lastBg = actualBg;
					actualBg = new ChildrenBG(lastBg.position.x + lastBg.size.x,lastBg.position.y, lastBg.size.x,lastBg.size.y);
					break;
					
				case 5:
					lastBg = actualBg;
					actualBg = new PresentBG(lastBg.position.x + lastBg.size.x,lastBg.position.y, lastBg.size.x,lastBg.size.y);
					break;
			}
		}
	} 
	
	protected void Touch()
	{
		if(touchEvents != null)
			for(int i = 0; i < touchEvents.size(); i++)
			{
				TouchEvent ev = touchEvents.get(i);
				if(ev.type == TouchEvent.TOUCH_DOWN)
				{
					if(ev.x < GameScreen.screenSize.x / 2)
					{
						this.player.TouchLeft();
					}
					
					if(ev.x > GameScreen.screenSize.x / 2)
					{
						this.player.TouchRight();
					}
				}
			}
	}
}
